//
//  MainTableViewController.m
//  dddddemo
//
//  Created by lanou3g on 15/11/3.
//  Copyright © 2015年 zhk. All rights reserved.
//

#import "MainTableViewController.h"
#import "AppDelegate.h"
#import "EditViewController.h"
#import "ModelFirst.h"
#import "DataSqlite.h"
#import "MyTableViewCell.h"
#import "checkViewController.h"
#import "PlaySound.h"
#import "XSportLight.h"
@interface MainTableViewController ()

@property(nonatomic,strong)PlaySound * plays;

@end

@implementation MainTableViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.plays = [[PlaySound alloc]initForPlayingSoundEffectWith:@"di.aif"];
    
    [[DataSqlite shareDataSqlite] openDataSqlite];
    
    [[DataSqlite shareDataSqlite] creatTable];
    
    [self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.navigationItem.title = @"Diary";
    
    self.view.backgroundColor = [UIColor colorWithRed:255 / 255.0 green: 211 / 255.0 blue: 155 / 255.0 alpha:1.0];

                                 
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftbarbutton.png"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftAction)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"rightbarbutton.png"] style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction)];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"XSLight"] isEqual:@(YES)]) {
        return;
    }else
    {
        XSportLight * sportLight = [[XSportLight alloc]init];
        sportLight.messageArray = @[@"点击这里开始您第一条记录吧~"];
        sportLight.rectArray = @[[NSValue valueWithCGRect:CGRectMake(CGRectGetMaxX(self.view.frame) - 40, 35, 50, 50)]];
        [self presentViewController:sportLight animated:YES completion:^{
        }];
        [[NSUserDefaults standardUserDefaults] setValue:@(YES) forKey:@"XSLight"];
    }
}



- (void)rightAction
{
    EditViewController * eVC = [[EditViewController alloc]init];
    [self.navigationController pushViewController:eVC animated:YES];
}



- (void)leftAction
{
    
    [self.plays play];
    
    // 个人理解  通过代理判断抽屉的开关状态  从而控制抽屉视图的开和关
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tempAppDelegate.leftSlideVC.closed) {
        [tempAppDelegate.leftSlideVC openLeftView];
    }else
    {
        [tempAppDelegate.leftSlideVC closeLeftView];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC setPanEnabled:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC setPanEnabled:YES];
    self.dataArray = [NSMutableArray arrayWithArray:[[DataSqlite shareDataSqlite] findAll]];
    [self.tableView reloadData];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CGRectGetHeight(self.tableView.frame) / 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    ModelFirst * m = self.dataArray[self.dataArray.count - indexPath.row - 1];
    
    cell.contentView.backgroundColor =[UIColor colorWithRed:255 / 255.0 green: 211 / 255.0 blue: 155 / 255.0 alpha:1.0];

    
    
    cell.titleLabel.text = m.title;
    
    cell.detaiLabel.text = m.content;
    cell.detaiLabel.font = [UIFont systemFontOfSize:13.0f];
    cell.detaiLabel.numberOfLines = 0;
    
    cell.detaiLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    
    cell.timeLabel.text = m.currentTime;
    
    cell.timeLabel.font = [UIFont systemFontOfSize:10.0f];
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelFirst * m = self.dataArray[self.dataArray.count - indexPath.row - 1];
    checkViewController * cVC = [[checkViewController alloc]init];
    cVC.title = m.title;
    [self.navigationController pushViewController:cVC animated:YES];
}


@end
